// 插件可以告诉 Rollup 如何查找外部模块
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import babel from 'rollup-plugin-babel';
// 多入口
import multi from '@rollup/plugin-multi-entry';
import browsersync from 'rollup-plugin-browsersync';
import html from '@rollup/plugin-html';
// 支持json文件导入
import json from '@rollup/plugin-json';
import { terser } from 'rollup-plugin-terser';
import postcss from 'rollup-plugin-postcss';
import autoprefixer from 'autoprefixer';
import cssnano from 'cssnano';
import replace from 'rollup-plugin-replace'
import copy from 'rollup-plugin-copy'
import template from './scripts/template';
import babelConfig from './scripts/babel';
const iconfont = require('rollup-plugin-iconfont');
const ENV = process.env.BUILD;
const isPROD = (ENV === 'production');
const rollupConfig = {
  treeshake: false,
  input: isPROD ? ['./src/**/*.js'] : './test/index.js',
  sourceMap: !isPROD,
  output: {
    legacy: true,
    name: '',
    file: `./dist/index${isPROD ? '.min' : ''}.js`,
    format: 'iife'
  }
};
const commonPlugins = [
  resolve({
    jsnext: true, // 该属性是指定将Node包转换为ES2015模块
    // main 和 browser 属性将使插件决定将那些文件应用到bundle中
    main: true, // Default: true
    browser: true // Default: false
  }),
  commonjs(),
  babel(babelConfig),
  multi(),
  json(),
  iconfont(),
  replace({
    'process.env.NODE_ENV': `'${ENV}'`,
  }
  ),
  postcss({
    extract: true, // 是否提取css, 可指定输出到具体路径
    plugins: [autoprefixer, cssnano],
    sourceMap: !isPROD,
    // modules: true,
  }),
  copy({
    targets: [
      { src: 'src/static/fonts/*.{woff,eot,svg,ttf,woff2}', dest: 'dist' },
    ]
  })
];

if (isPROD) {
  rollupConfig.plugins = commonPlugins.concat([
    terser({
      keep_fnames: true,
      ie8: true,
      compress: {
        drop_debugger: true,
        drop_console: true,
        inline: false
      },
      warnings: false,
      mangle: true
    })
  ]);
} else {
  rollupConfig.plugins = commonPlugins.concat([
    browsersync({
      server: ['dist'] //监听文件夹;
    }),
    html({ template }),
  ]);
}
export default rollupConfig;
